
#include "BlockQueue.hpp"
#include "time.h"
#include <cstdlib>
#include <unistd.h>
#include"Task.hpp"
using namespace ns_blockqueue;
using namespace std;
using namespace ns_task;

void *consumer(void *args)
{
    BlockQueue<Task> *bq = (BlockQueue<Task> *)args;
    while (true)
    {
        // sleep(1);
        // int date = 0;
        // bq->Pop(&date);
        // cout << "消费者消费了一个数据:" << date << endl;

        //处理Task代码
        Task t;
        bq->Pop(&t);
        t();
    }
}

void *producter(void *args)
{
    BlockQueue<Task> *bq = (BlockQueue<Task> *)args;
    char oparr[]="+-*%/";
    while (true)
    {

        // int date = rand() % 10 + 1;
        // cout << "生产者生产了一个数据：" << date << endl;
        // bq->Push(date);

        //处理Task代码
        //1.产生数据
        int x=rand()%512+1;
        int y=rand()%7+1;
        char op=oparr[rand()%5];
        cout<<"生产者产生了一个数据"<<x<<op<<y<<"=?"<<endl;
        Task t(x,y,op);
        //2.发送数据过去
        bq->Push(t);
        sleep(1);
    }
}

// 生产者消费者模型 ---进程间通信的原理
int main()
{
    srand((long long)time(nullptr));
    BlockQueue<Task> *bq = new BlockQueue<Task>();

    pthread_t c, p;
    pthread_create(&c, nullptr, consumer, (void *)bq);
    pthread_create(&p, nullptr, producter, (void *)bq);

    pthread_join(c, nullptr);
    pthread_join(p, nullptr);
}